// pages/upload/upload_purchase.js
const app = getApp();
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    needed_nums: '',
    address: '',
    detail: '',
    iamges_list: [],
    img_url: '',
    class_index: 0,
    kinds: '',
    phone: '',
  },


  // 发布
  jump: function () {
    var that = this
    console.log(this.data)
    wx.showToast({
      title: '上传中',
      icon: 'loading',
    })
    wx.cloud.uploadFile({
      cloudPath: "images_folder/" + Math.random().toString(),
      filePath: that.data.iamges_list[0], // 文件路径
      success: function (res) {
        // get resource ID
        that.setData({
          img_url: res.fileID,
        })
        if (that.data.img_url) {
          that.upDatabase();
        }
        wx.switchTab({
          url: '/pages/purchase/purchase',
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '请上传图片',
          icon: 'error',
        })

      },
    })
  },

  // 更新数据库
  upDatabase: function () {
    var that = this;
    db.collection('purchase').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        address: that.data.address,
        title: that.data.title,
        detail: that.data.detail,
        needed_num: that.data.needed_nums,
        detail_kind: that.data.detail_kind,
        img_url: that.data.img_url,
        phone: that.data.phone,
        upload_time: new Date(),
      },
      success: function () {
        wx.showToast({
          title: '成功发布',
          icon: 'success'
        })
      }
    })
  },

   // 点我选择分类
   chooseClass: function (e) {
    this.setData({
      class_index: e.detail.value,
      detail_kind: this.data.kinds[e.detail.value],
    })
  },

    // 选择图片，获得临时地址
    chooseImages: function (e) {
      var that = this
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          that.setData({
            iamges_list: res.tempFilePaths
          });
        },
      });
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  // 填写信息上传
  input_title: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  input_choose: function (e) {
    this.setData({
      kind: e.detail.value
    })
  },
  input_nums: function (e) {
    this.setData({
      needed_nums: e.detail.value
    })
  },
  input_address: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  input_detail: function (e) {
    this.setData({
      detail: e.detail.value
    })
  },
  input_phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this

    db.collection('classify').doc('classify_001').get({
      success: function (res) {
        // res.data 包含该记录的数据
        that.setData({
          kinds: res.data.kinds,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})